home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: while loop problem
- Date: 12 Mar 1996 13:00:24 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4i4ol8INN3i4@keats.ugrad.cs.ubc.ca>
- References: <Pine.OSF.3.91.960312133449.7844B-100000@io.UWinnipeg.ca>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <Pine.OSF.3.91.960312133449.7844B-100000@io.UWinnipeg.ca>,
- Bill Simpson <wsimpson@uwinnipeg.ca> wrote:
- >Consider the following code fragment:
- >
- >y=2000; z=1000;
- >x=0;
- >while(x<=XMAX)
- > {
- > x+=(int) erand(mean);
- > setdot(x,y,z);
- > }
- >
- >It plots a line of dots that are randomly (exponentially) spaced, giving
- >a 1D spatial Poisson process.
- >The problem is that dot x coordinates can only be between 0 and XMAX.
- >The above code will also attempt to plot one point at an x value >XMAX
- >before it terminates.
- >
- >Is there a nice way to write the code so it works properly?
-
- Yes. Exploit the relationship between the poisson probability and the
- exponential probability. You are using exponential probability to model the
- time between poisson events. Why not model the poisson process in a direct
- way?
-
- A quick and dirty answer is to restructure your loop so that the test for
- x<=XMAX is done before the next integration of erand() into x.
-
- x = erand(mean);
-
- do {
- setdot(x,y,z);
- x += erand(mean);
- } while (x <= XMAX);
-
- >The only way I have thought of is
- >y=2000; z=1000;
- >x=0;
- >while(x<=XMAX)
- > {
- > x+=(int) erand(mean);
- > if(x<=XMAX)
- > setdot(x,y,z);
- > }
-
- Crude.
-
- >which seems very clumsy since the same test is done twice.
-
- Right you are. You are on the right track; that is why there are other looping
- structures such as for() and do while().
- --
-
-